package work.yixin.database.test.config;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

@Configuration
public class DataSourcesConfig {
	
	
	/**
	 * 注册DruidServlet
	 *http://localhost:8080/druid/index.html
	 * @return
	 */
	@Bean
	public ServletRegistrationBean<StatViewServlet> druidServletRegistrationBean() {
	  ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<StatViewServlet>();
	  servletRegistrationBean.setServlet(new StatViewServlet());
	  servletRegistrationBean.addUrlMappings("/druid/*");
	  //登录查看信息的账号密码.
	  servletRegistrationBean.addInitParameter("loginUsername", "admin");
	  servletRegistrationBean.addInitParameter("loginPassword", "123456");
	  return servletRegistrationBean;
	}
	 
	/**
	 * 注册DruidFilter拦截
	 *
	 * @return
	 */
	@Bean
	public FilterRegistrationBean<WebStatFilter> druidFilterRegistrationBean() {
	  FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<WebStatFilter>();
	  filterRegistrationBean.setFilter(new WebStatFilter());
	  Map<String, String> initParams = new HashMap<String, String>();
	  //设置忽略请求
	  initParams.put("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
	  filterRegistrationBean.setInitParameters(initParams);
	  filterRegistrationBean.addUrlPatterns("/*");
	  return filterRegistrationBean;
	}
	 
	 	@Bean
	    @ConfigurationProperties("spring.datasource.druid.first")
	    public DataSource firstDataSource(){
	        return DruidDataSourceBuilder.create().build();
	    }

	 	public DataSource allDataSource() {
	 		DruidDataSource druidDataSource = new DruidDataSource();
//	 		  druidDataSource.setUsername(username);
//	 		  druidDataSource.setPassword(password);
//	 		  druidDataSource.setUrl(dbUrl);
//	 		  druidDataSource.setFilters("stat,wall");
//	 		  druidDataSource.setInitialSize(initialSize);
//	 		  druidDataSource.setMinIdle(minIdle);
//	 		  druidDataSource.setMaxActive(maxActive);
//	 		  druidDataSource.setMaxWait(maxWait);
//	 		  druidDataSource.setUseGlobalDataSourceStat(true);
//	 		  druidDataSource.setDriverClassName(driverClassName);
	 		  return druidDataSource;
	 	}
	    @Bean
	    @ConfigurationProperties("spring.datasource.druid.second")
	    public DataSource secondDataSource(){
	        return DruidDataSourceBuilder.create().build();
	    }

	    @Bean
	    @Primary
	    public DynamicDataSource dataSource(DataSource firstDataSource, DataSource secondDataSource) {
	        Map<Object, Object> targetDataSources = new HashMap<>();
	        targetDataSources.put(DataSourceType.FIRST, firstDataSource);
	        targetDataSources.put(DataSourceType.SECOND, secondDataSource);
	        return new DynamicDataSource(firstDataSource, targetDataSources);
	    }

}
